
<?php
class upload_notes extends CI_Model {

  function __construct()
    {
         parent::__construct();
  	}
    
  	function get_notes($tot_num, $offset,$email)
    {        
        $this->db->where('user_id',$this->session->userdata('user_id'));
        $query = $this->db->get('notes', $tot_num, $offset);
        
        $ret['records']=$query->result();
        $this->db->where('user_id',$this->session->userdata('user_id'));
        $ret['num_rows'] = $this->db->get('notes')->num_rows();
        return $ret;
    }	
   function upload($data)
    {
        
        $this->db->insert('notes',$data); 

    }
    
    function download_increment($val,$id)
    {
        $this->db->where('notes_id',$id);
        $this->db->update('notes',array('downloads' => $val));
    }
      function get_records($tot_num, $offset,$sort_by, $sort_order)
    {
          
        $this->db->order_by($sort_by, $sort_order);
          $this->db->where('active','1'); 
    	$query = $this->db->get('notes', $tot_num, $offset);
    	 $ret['records'] = $query->result();



      //for total num rows
     
      $this->db->order_by($sort_by, $sort_order);

      $ret['num_rows'] = $this->db->get('notes')->num_rows(); 
      return $ret;
    }	
	 function get_records1($tot_num, $offset,$sort_by, $sort_order)
    {
          
        $this->db->order_by($sort_by, $sort_order);
          $this->db->where('active','1'); 
    	$query = $this->db->get('notes', $tot_num, $offset);
    	return $query->result();
         
     }
    function get_note($id)
    {

        $this->db->select('*');
        $this->db->from('user AS A');// I use aliasing make joins easier
        $this->db->join('notes AS C', 'A.user_id = C.user_id', 'INNER');
        $this->db->where('C.notes_id',$id);
        $query = $this->db->get();
        return $query->result();
    }
     public function search($tot_num, $offset, $sort_by, $sort_order,$search_q)
    {
      $this->db->order_by($sort_by, $sort_order);

     

      $this->db->like('note_title', $search_q);
      //tokenize
      $token = strtok($search_q," ");
      while ($token!=false) 
      {
        $this->db->or_like('note_title', $token);
          $this->db->or_like('semester', $token);
           $this->db->or_like('author', $token);
        $this->db->or_like('disc', $token);
        $this->db->or_like('faculty', $token);  
        $token = strtok(" ");
      }
        
      //user data
      $this->db->join('user','user.user_id=notes.user_id');

      $query = $this->db->get('notes', $tot_num, $offset);
      $ret['records'] = $query->result();



      //for total num rows
      $this->db->order_by($sort_by, $sort_order);
      $this->db->like('note_title', $search_q);
      //tokenize
      $token = strtok($search_q," ");
      while ($token!=false) 
      {
         $this->db->or_like('note_title', $token);
          $this->db->or_like('semester', $token);
           $this->db->or_like('author', $token);
        $this->db->or_like('disc', $token);
        $this->db->or_like('faculty', $token);  
        $token = strtok(" ");
      }

      $ret['num_rows'] = $this->db->get('notes')->num_rows(); 
      return $ret;
    }

    function get_edit_notes($id)
    {
       
    	$this->db->where('notes_id',$id);
        $query = $this->db->get('notes');
        return $query->result();
        
    }	
     function update_note_record($data,$note_id)
    {
       
    	$this->db->where('notes_id',$note_id);
        $this->db->update('notes',$data);
        
    }	
     function image_path_to_delete($note_id,$img_id)
    {
       
     $this->db->select($img_id); 
     $this->db->from('notes');   
     $this->db->where('notes_id',$note_id);
     $result = $this->db->get();
     foreach ($result->result_array() as $row)
     {
         return $row[$img_id];
     }
    }	
    function delete_notes_record($ad_id)
    {
       
    	$this->db->where('notes_id',$ad_id);
        $this->db->delete('notes'); 
        
    }
    function active_notes($notes_id,$data)
    {
       
    	$this->db->where('notes_id',$notes_id);
        $this->db->update('notes',$data);
        
    }

  
}
?>